package com.person.jc.web.finance;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.Constants;
import com.person.jc.entity.TsUser;
import com.person.jc.service.finance.BsOrderBillingService;
import com.person.jc.utils.DateUtils;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.SessionKit;
import com.person.jc.utils.Utils;

public class BsOrderBillingController extends Controller {
	
	public void init(){
		setAttr("menuId", getPara("menuId"));
		setAttr("startDate",DateUtils.formatDate(DateUtils.addMonths(new Date(), -1), "yyyy-MM-dd"));		
		setAttr("endDate",DateUtils.getDate());
		render("main.jsp");
	}
	
	public void list(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		List<Record> list = BsOrderBillingService.me.getList(map);
		setAttr("list", list);
		render("list.jsp");
	}
	
	public void edit(){
		String billingId = getPara("billingId");
		String view = getPara("view");
		if(Utils.isNotEmpty(billingId)){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("billingId", billingId);
			List<Record> list = BsOrderBillingService.me.getList(map);
			if(list.size() != 0){
				setAttr("obj", list.get(0));
			}
			List<Record> detail = BsOrderBillingService.me.getBillingDetail(billingId);
			setAttr("details", detail);
		}else{
			setAttr("detailsJson", "[]");
		}
		setAttr("view", view);
		render("edit.jsp");
	}
	/**
	 * 确认开票
	 */
	public void save(){
		TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		map.put("optUser", tsUser.getUserId());
		BsOrderBillingService.me.pay(map);
		renderText("success");
	}
	
	/**
	 * 跳转取票页面
	 */
	public void tackTicket(){
		String billingId = getPara("billingId");
		String view = getPara("view");
		if(Utils.isNotEmpty(billingId)){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("billingId", billingId);
			List<Record> list = BsOrderBillingService.me.getList(map);
			if(list.size() != 0){
				setAttr("obj", list.get(0));
			}
		}else{
			setAttr("obj", "");
		}
		setAttr("view", view);
		render("tackTicket.jsp");
	}
	
	/**
	 * 确认取票
	 */
	public void doTackTickets(){
		TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		map.put("optUser", tsUser.getUserId());
		BsOrderBillingService.me.doTackTickets(map);
		renderText("success");
	}
	
	/**
	 * 跳转回款界面
	 */
	public void showPayBack(){
		String billingId = getPara("billingId");
		String view = getPara("view");
		if(Utils.isNotEmpty(billingId)){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("billingId", billingId);
			List<Record> list = BsOrderBillingService.me.getList(map);
			if(list.size() != 0){
				BigDecimal alreadyPaid = BsOrderBillingService.me.getPaymentAmount(billingId);
				setAttr("obj", list.get(0));
				setAttr("alreadyPaid",alreadyPaid);
			}
			List<Record> detail = BsOrderBillingService.me.getPayBackDetail(billingId);
			setAttr("details", detail);
		}else{
			setAttr("obj", "");
		}
		setAttr("view", view);
		render("payBack.jsp");
	}
	
	/**
	 * 确认回款
	 */
	public void doPayBack(){
		TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		map.put("optUser", tsUser.getUserId());
		BsOrderBillingService.me.doPayBack(map);
		renderText("success");
	}
	
	
	
}
